﻿using System.Windows.Controls;
using Infrastructure.ViewModels;
using UI.Controls.Dictionaries;

namespace UI.Dialogs.Dictionaries
{
    /// <summary>
    /// Publisher builder class
    /// </summary>
    public class PublisherBuilder : DictionaryBuilder
    {
        readonly PublisherGrid _grid;
        readonly PublisherDetailed _addPublisher;
        readonly PublisherDetailed _editPublisher;

        #region Overrides of DictionaryBuilder

        /// <summary>
        /// Initializes a new instance of the <see cref="PublisherBuilder"/> class.
        /// </summary>
        /// <param name="form">The dictionary form.</param>
        public PublisherBuilder(DictionaryFormBase form) : base(form)
        {
            form.Title = "Publishers";

            _addPublisher = new PublisherDetailed();
            _editPublisher = new PublisherDetailed();

            _grid = new PublisherGrid();
            _grid.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
                                          {
                                              if (args.AddedItems.Count > 0)
                                              {
                                                  _editPublisher.Item = args.AddedItems[0] as CompanyViewModel;
                                              }
                                          };
            _grid.SelectionChanged += SelectionChanged;

            Form.AddClicked += delegate { Save(); };
            Form.SaveClicked += delegate { Update(); };
            
        }

        /// <summary>
        /// Updates the grid.
        /// </summary>
        /// <param name="operation">The operation.</param>
        /// <param name="item">The item.</param>
        public override void UpdateGrid(UpdateOperation operation, object item)
        {
            _grid.UpdateGrid(operation, item);
        }


        /// <summary>
        /// Selection changed event handler.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        protected override void SelectionChanged(object sender, SelectionChangedEventArgs args)
        {
            base.SelectionChanged(sender, args);

            if (args.AddedItems.Count > 0)
            {
                _editPublisher.Item = args.AddedItems[0] as CompanyViewModel;
            }
        }

        /// <summary>
        /// Builds the dictionary form and functionality.
        /// </summary>
        public override void Build()
        {
            Form.GridControl = _grid;
            Form.AddControl = _addPublisher;
            Form.EditControl = _editPublisher;
        }

        /// <summary>
        /// Updates the selected item.
        /// </summary>
        public override void Update()
        {
            _editPublisher.Save.Execute(null);
        }

        /// <summary>
        /// Saves the new item.
        /// </summary>
        public override void Save()
        {
            _addPublisher.Save.Execute(null);
        }

        /// <summary>
        /// Sets the selected item.
        /// </summary>
        /// <param name="item">The item.</param>
        public override void SetSelected(object item)
        {
            _grid.Selected = item;
        }


        #endregion

        
    }
}
